package ${ConfigPackage};

import java.nio.ByteBuffer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.*;

import com.myapp.data.define.BaseBean;
import com.myapp.db.config.ann.TableDefine;
import com.myapp.db.config.define.UpdateData;
import com.myapp.db.dao.manual.${Name?cap_first}Dao;

/**
 * 文件名：${Name?cap_first}Entity.java
 * <p>
 * 功能：${Remark}
 * <p>
 * 作者：Builded by AppTools.
 */
@TableDefine(name = "${Name?cap_first}", dao = ${Name?cap_first}Dao.class)
public class ${Name?cap_first}Entity extends BaseBean {
<#if FieldBeans?size!=0>
	<#list FieldBeans as field>
	// ${field.cn}
	<#if field.type == "int" || field.type == "smallint">
	private AtomicInteger ${field.en} = new AtomicInteger(0);
	</#if> 
	<#if field.type == "bigint">
	private AtomicLong ${field.en} = new AtomicLong(0);
	</#if>
	<#if field.type == "varchar">
	private String ${field.en} = null;
	</#if>
	
	</#list>
</#if>

<#if FieldBeans?size!=0>
	<#list FieldBeans as field>
	<#if field.type == "int" || field.type == "smallint">
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public int get${field.en?cap_first}() {
		return ${field.en}.get();
	}

	/**
	 * 原子增加${field.cn}
	 * 
	 * @param ${field.en} 要加的值（正数）
	 */
	public void add${field.en?cap_first}(int ${field.en}) {
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.addAndGet(${field.en});
	}
	
	/**
	 * 原子减少${field.cn}
	 * 
	 * @param ${field.en} 要减的值（正数）
	 */
	public void minus${field.en?cap_first}(int ${field.en}) {
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.addAndGet(${field.en}*-1);
	}
	
	/**
	 * 直接设置${field.en}的值
	 * 
	 * @param ${field.en} 要设置的新值
	 */
	public void set${field.en?cap_first}(int ${field.en}){
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.set(${field.en});
	}
	</#if>
	<#if field.type == "bigint">
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public long get${field.en?cap_first}() {
		return this.${field.en}.get();
	}

	/**
	 * 原子增加${field.cn}
	 * 
	 * @param ${field.en} 要加的值（正数）
	 */
	public void add${field.en?cap_first}(long ${field.en}) {
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.addAndGet(${field.en});
	}

	/**
	 * 原子减少${field.cn}
	 * 
	 * @param ${field.en} 要减的值（正数）
	 */
	public void minus${field.en?cap_first}(long ${field.en}) {
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.addAndGet(${field.en}*-1);
	}
	
	/**
	 * 直接设置${field.en}的值
	 * 
	 * @param ${field.en} 要设置的新值
	 */
	public void set${field.en?cap_first}(long ${field.en}){
		UpdateData data = new UpdateData(this.${field.en}.get(), ${field.en});
		this.getOldData().put("${field.en}", data);
		this.${field.en}.set(${field.en});
	}
	</#if>
	<#if field.type == "varchar">
	/**
	 * 获取值
	 * 
	 * @return 要返回的值
	 */
	public String get${field.en?cap_first}() {
		return this.${field.en};
	}

	/**
	 * 直接设置${field.en}的值
	 * 
	 * @param ${field.en} 要设置的新值
	 */
	public void set${field.en?cap_first}(String ${field.en}) {
		if(this.${field.en} == null){
			UpdateData data = new UpdateData("", ${field.en});
			this.getOldData().put("${field.en}", data);
		}else{
			UpdateData data = new UpdateData(this.${field.en}, ${field.en});
			this.getOldData().put("${field.en}", data);
		}
		this.${field.en} = ${field.en};
	}
	</#if>
	
	</#list>
</#if>

	@Override
	public void parseEntity(ResultSet rest) throws SQLException {
		// id
		this.setId(rest.getLong(1));
<#if FieldBeans?size!=0>
	<#list FieldBeans as field>
		// ${field.cn}
		<#if field.type == "int" || field.type == "smallint">
		this.${field.en}.set(rest.getInt(${field_index+2}));
		</#if> 
		<#if field.type == "bigint">
		this.${field.en}.set(rest.getLong(${field_index+2}));
		</#if>
		<#if field.type == "varchar">
		String tmp = rest.getString(${field_index+2});
		if (!"null".equals(tmp)) {
			this.${field.en} = tmp;
		}
		</#if>
	</#list>
</#if>
	}
	
	
	/**
	 * 将本对象变化的数据序列化
	 * 
	 * @return 返回序列化后的数据
	 */
	@Override
	public byte[] serialization() {
	ByteBuffer data = ByteBuffer.allocate(1024);
<#if FieldBeans?size!=0>
	<#list FieldBeans as field>
		if (this.getOldData().containsKey("${field.en}")) {
			<#if field.type == "int" || field.type == "smallint">
			data = this.checkCapacity(data, 4);
			data.put((byte) ${field_index});
			data.putInt(${field.en}.get());
			</#if> 
			<#if field.type == "bigint">
			data = this.checkCapacity(data, 8);
			data.put((byte) ${field_index});
			data.putLong(${field.en}.get());
			</#if>
			<#if field.type == "varchar">
			byte[] tmp=${field.en}.getBytes();
			data = this.checkCapacity(data, tmp.length);
			data.put((byte) ${field_index});
			data.putInt(tmp.length);
			data.put(tmp);
			</#if>
		}
	</#list>
</#if>
		data.flip();
		return data.array();
	}
	
	
	/**
	 * 反序列化
	 * 
	 * @param data 要反序列化的数据
	 * 
	 * @return 返回序列化出来的对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ${Name?cap_first}Entity unSerialization(byte[] data) {
		${Name?cap_first}Entity ${Name}Entity = new ${Name?cap_first}Entity();
		if (data.length > 0) {

			ByteBuffer buffer = ByteBuffer.wrap(data);
			buffer.flip();

			byte type = 0;
			while (buffer.position() < buffer.limit()) {
				type = buffer.get();
<#if FieldBeans?size!=0>
		<#list FieldBeans as field>
			if (type ==  ${field_index}) {
			<#if field.type == "int" || field.type == "smallint">
				this.set${field.en?cap_first}(buffer.getInt());
			</#if> 
			<#if field.type == "bigint">
				this.set${field.en?cap_first}(buffer.getLong());
			</#if>
			<#if field.type == "varchar">
				int length=buffer.getInt();
				byte[] tmp=new byte[length];
				buffer.get(tmp);
				this.set${field.en?cap_first}(new String(tmp));
			</#if>	
			}
			
		</#list>
</#if>
			}
		}

		return ${Name}Entity;
	}
}
